﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class inc_top : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        if(Session["user"] != null)
        {
            this.LblUserName.Text ="您好:" + (Session["user"] as User).UserName;
        }
    }
    protected void CalendarTimer_Tick(object sender, EventArgs e)
    {
        this.Calendar();
    }
    /// <summary>
    /// 日程查找
    /// </summary>
    public void Calendar()
    {
        User curLoginUser = (User) Session["user"];
        CalendarArrangeInfos caInfos = CalendarArrangeInfos.instance();
        if(caInfos.Cainfos[curLoginUser.LoginName] == null)
        {
            this.addCurLoginUserCalendarInfo();
        }
        else
        {
            List<UserCalendarArrangetb> list = (List<UserCalendarArrangetb>) caInfos.Cainfos[curLoginUser.LoginName];
            if(list.Count > 0)
            {
                string script = "";
                foreach(UserCalendarArrangetb ucatb in list)
                {
                    DateTime startTime = DateTime.Parse(ucatb.CalendarArrangeDate.ToShortDateString() + " " + ucatb.CalendarArrangeStartTime);
                    DateTime endTime = DateTime.Parse(ucatb.CalendarArrangeDate.ToShortDateString() + " " + ucatb.CalendarArrangeEndTime);
                    if(startTime <= DateTime.Now && endTime >= DateTime.Now)
                    {
                        TimeSpan minutes = DateTime.Now - startTime;
                        if(minutes.Minutes % ucatb.CalendarArrangeIntervalMinute == 0)
                        {
                            //ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "window.open('ShowModel.aspx?ca_id=" 
                            //    + ucatb.CalendarArrangeId + "', '', 'status:no;center:yes;scroll:0;resizable:no;help:no;dialogWidth:570px;dialogHeight:445px');", true);
                            script += "window.open('../CalendarArrange/ShowModel.aspx?ca_id=" + ucatb.CalendarArrangeId + "');";
                        }
                    }
                }
                if(!"".Equals(script))
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", script, true);
                }
            }
        }
    }

    private void addCurLoginUserCalendarInfo()
    {
        DateTime curDt = DateTime.Parse(DateTime.Now.ToShortDateString());
        User curLoginUser = (User) Session["user"];
        CalendarArrangeInfos caInfos = CalendarArrangeInfos.instance();
        UserCalendarArrangetbService ucatbService = new UserCalendarArrangetbService();
        List<UserCalendarArrangetb> list = ucatbService.findCurDateAllByUserLoginNameAndCurDate(curLoginUser.LoginName, curDt);
        if(list == null)
        {
            list = new List<UserCalendarArrangetb>();
        }
        if(caInfos.Cainfos[curLoginUser.LoginName] == null)
        {
            caInfos.AddCaInfos(curLoginUser.LoginName, list);
        }
        else
        {
            List<UserCalendarArrangetb> lists = (List<UserCalendarArrangetb>) caInfos.Cainfos[curLoginUser.LoginName];
            if(lists.Count > 0)
            {
                if(lists[0].CalendarArrangeDate.Date != curDt)
                {
                    caInfos.Cainfos.Remove(curLoginUser.LoginName);
                    caInfos.AddCaInfos(curLoginUser.LoginName, list);
                }
            }
        }
    }
}
